// pages/teamListPage/teamListPage.js
/*
  需要完成的操作
  1.设为默认团队时 修改用户默认团队ID
  2.团队成员管理，需要获取加入团队的用户列表
  3.邀请团队成员
  4.编辑团队，需要修改团队名称
  5.解散团队，需要删除团队
  6.退出团队，需要把用户移出团队
*/
import {
  request
} from '../../request/index.js';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
        id: 0,
        value: "我管理的团队",
        isActive: true
      },
      {
        id: 1,
        value: "我加入的团队",
        isActive: false
      }
    ],
    manageTeamList: [],
    joinTeamList: [],
    defaultTeamId: '',
    // 标记是编辑图层还是添加图层
    flag: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.setData({
      manageTeamList: app.globalData.manageTeamList,
      joinTeamList: app.globalData.joinTeamList
    })
    if(app.globalData.defaultTeamId){
      this.setData({
        defaultTeamId: app.globalData.defaultTeamId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.hideModal()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    app.globalData.teamListPageHideFlag = true
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.data.defaultTeamId) {
      request({
        url: '/map/getMapListByTeamId/' + this.data.defaultTeamId
      }).then(res => {
        app.globalData.mapGlobal = res.data.data
      })
    }
    app.globalData.teamListPageHideFlag = true
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  handleTabsItemChange(e) {
    const {
      index
    } = e.detail
    let {
      tabs
    } = this.data
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
    this.setData({
      tabs
    })
  },
  add_team() {
    // 编辑图层
    this.setData({
      flag: 'add'
    })
    this.setData({
      showModal: true
    })
  },
  close_mask: function () {
    this.setData({
      showModal: false
    })
  },
  // 输入字段
  inputModify: function (e) {
    this.setData({
      modifyLayerName: e.detail.value
    })
  },
  manageTeamSet(e) {
    this.setData({
      selectedTeamId: e.currentTarget.dataset.teamid
    })
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  joinTeamSet(e) {
    this.setData({
      selectedTeamId: e.currentTarget.dataset.teamid
    })
    // 显示遮罩层
    var animation2 = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation2
    animation2.translateY(300).step()
    this.setData({
      animationData2: animation2.export(),
      showModalStatus2: true
    })
    setTimeout(function () {
      animation2.translateY(0).step()
      this.setData({
        animationData2: animation2.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal2: function () {
    // 隐藏遮罩层
    var animation2 = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation2 = animation2
    animation2.translateY(300).step()
    this.setData({
      animationData2: animation2.export(),
    })
    setTimeout(function () {
      animation2.translateY(0).step()
      this.setData({
        animationData2: animation2.export(),
        showModalStatus2: false
      })
    }.bind(this), 200)
  },
  // 设为默认团队
  team_moren: async function (e) {
    this.setData({
      defaultTeamId: this.data.selectedTeamId
    })
    app.globalData.defaultTeamId = this.data.defaultTeamId
    // 发起请求
    let res = await request({
      url: '/teamuser/updateDefaultTeam?teamId=' + app.globalData.defaultTeamId + '&userId=' + app.globalData.userIdGlobal,
      method: 'PUT'
    })
    this.hideModal()
    this.hideModal2()
  },
  // 新建团队
  async modifyLayerName() {
    // console.log(this.data.modifyLayerName)
    if (this.data.flag == 'add') {
      wx.showLoading({
        title: '加载中',
        mask: true
      });
      let res = await request({
        url: '/teamuser/createTeam?teamName=' + this.data.modifyLayerName + '&userId=' + app.globalData.userIdGlobal,
        method: 'POST'
      }, false)
      let p1 = await request({
        url: '/teamuser/getManageTeamList/' + app.globalData.userIdGlobal
      }, false)
      app.globalData.manageTeamList = p1.data.data
      this.setData({
        manageTeamList: app.globalData.manageTeamList
      })
      this.close_mask()
      wx.hideLoading()
    } else if (this.data.flag == 'edit') {
      wx.showLoading({
        title: '加载中',
        mask: true
      });
      let sendTmp = {}
      sendTmp['teamName'] = this.data.modifyLayerName
      let res = await request({
        url: '/team/updateTeamName?teamId=' + this.data.selectedTeamId,
        method: 'PUT',
        data: sendTmp
      }, false)
      let p1 = await request({
        url: '/teamuser/getManageTeamList/' + app.globalData.userIdGlobal
      }, false)
      app.globalData.manageTeamList = p1.data.data
      this.setData({
        manageTeamList: app.globalData.manageTeamList
      })
      this.close_mask()
      wx.hideLoading()
    }
  },
  // 编辑团队
  team_edit() {
    this.setData({
      flag: 'edit'
    })
    this.setData({
      showModal: true
    })
    this.hideModal()
  },
  // 退出团队
  team_delete(e) {
    console.log('退出')
  },
  // 管理团队
  team_manage() {
    wx.navigateTo({
      url: '../teamMemberManagePage/teamMemberManagePage?teamId=' + this.data.selectedTeamId,
    })
  }
})